package com.kgc.sbt.provider;

import com.alibaba.dubbo.config.annotation.Service;
import com.kgc.sbt.api.FavoriteApi;
import com.kgc.sbt.domain.Favorite;
import com.kgc.sbt.mapper.FavoriteMapper;
import org.springframework.beans.factory.annotation.Autowired;


import java.util.Date;


/**
 * Created on 2021/6/23 0023.
 * <p>
 * Author: px
 * <p>
 * Description: 收藏接口实现类
 */
@Service
public class FavoriteProvider implements FavoriteApi {

    @Autowired
    private FavoriteMapper favoriteMapper;



    /*@Override
    public List<Favorite> queryFavoriteByUserId(Integer userId) {
        return favoriteMapper.selectByExample();
    }*/
    /**
     * @Author px
     * @Date 2021/6/24 0024
     * @Param [goodsId, userId]
     * @Description: 根据商品id和用户id添加收藏
     **/
    @Override
    public Boolean addFavoriteById(Integer goodsId, Integer userId) {

        Integer isExist = favoriteMapper.selectFavorite(userId, goodsId);

        if (isExist != 0 ){
            return false;
        } else {
            Favorite favorite = Favorite.builder().userId(userId).goodsId(goodsId).createTime(new Date()).build();
            return favoriteMapper.insert(favorite) > 0 ? true : false;
        }


    }

    /**
     * @Author px
     * @Date 2021/6/24 0024
     * @Param [favId]
     * @Description: 根据收藏id移出收藏
     **/
    @Override
    public Boolean delFavoriteById(Integer goodsId, Integer userId) {

        Integer isExist = favoriteMapper.selectFavorite(userId, goodsId);

        if (isExist != 0 ){
            return favoriteMapper.deleteById(userId, goodsId) > 0;
        } else {
            return false;
        }

    }



}
